﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Heyo.Launcher.Models.Data;
using HeyoUi.Models.Data.GameData;
using Newtonsoft.Json;

namespace Heyo.Launcher.Models
{
    internal class SettingModel
    {
        private static readonly string JsonPath = Environment.CurrentDirectory+ "/heyo.json";
        public static SettingJson Setting { get; }
        static SettingModel()
        {

            FileStream fs = File.Open(JsonPath, FileMode.OpenOrCreate);
            using (StreamReader reader = new StreamReader(fs, Encoding.UTF8))
            {
                string jsonStr = reader.ReadToEnd();
                Setting = JsonConvert.DeserializeObject<SettingJson>(jsonStr);
                if (Setting == null)
                {
                    fs.Close();
                    Setting = new SettingJson();
                    Save();
                }
            }
            fs.Close();
        }

        public static void Save()
        {
            FileStream fs = File.Open(JsonPath, FileMode.Create);
            using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8))
            {
                string jsonStr = JsonConvert.SerializeObject(Setting);
                writer.Write(jsonStr);
            }
            fs.Close();
        }
    }
}
